//
//  FirstLaunchViewController.m
//  ElectricityProject
//
//  Created by Jack on 14-9-26.
//  Copyright (c) 2014年 王洋. All rights reserved.
//

#import "FirstLaunchViewController.h"
#import "AppDelegate.h"

@interface FirstLaunchViewController ()<UIScrollViewDelegate>

@end

@implementation FirstLaunchViewController
@synthesize scrollViewM;
@synthesize pageControl;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self viewDisplay];
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"RECIEVENOT"];
}

- (void)viewDisplay
{
    
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"STATUS"];
    
    float Width=[UIScreen mainScreen].bounds.size.width;
    float Height=[UIScreen mainScreen].bounds.size.height;
    
    scrollViewM=[[UIScrollView alloc]initWithFrame:self.view.frame];
    scrollViewM.contentSize = CGSizeMake(Width*3, Height);
    scrollViewM.scrollEnabled = YES;
    scrollViewM.pagingEnabled=YES;
    scrollViewM.delegate = self;
    scrollViewM.showsHorizontalScrollIndicator = NO;
    NSArray *colorArr=@[[UIColor orangeColor],[UIColor yellowColor],[UIColor redColor]];
    for (int i=0; i<3; i++) {
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(Width*i, 0, Width, Height)];
        imageview.userInteractionEnabled=YES;
        imageview.backgroundColor=colorArr[i];
//        imageview.image=[UIImage imageNamed:[NSString stringWithFormat:@"guide_%i.png",i+1]];
        [scrollViewM addSubview:imageview];
        
        if (i==2) {
            UIButton *pressBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            pressBtn.frame=CGRectMake((self.view.frame.size.width-202)/2, Height-70, 202, 47);
//            [pressBtn setBackgroundImage:[UIImage imageNamed:@"exp.png"] forState:UIControlStateNormal];
            [pressBtn setTitle:@"立即体验" forState:UIControlStateNormal];
            [pressBtn addTarget:self action:@selector(goToHomeViewController) forControlEvents:UIControlEventTouchUpInside];
            [imageview addSubview:pressBtn];
        }
    }
    
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, Height-60, 80, 25)];
    pageControl.numberOfPages=3;
    pageControl.pageIndicatorTintColor=RGBACOLOR(9, 192 , 199, 0.4);
    pageControl.currentPageIndicatorTintColor=MainColor;
    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:scrollViewM];
    [self.view addSubview:pageControl];
}


-(void)goToHomeViewController
{
    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if(scrollView==scrollViewM){
        
        CGPoint offset = scrollView.contentOffset;
        pageControl.currentPage = offset.x / (self.view.bounds.size.width); //计算当前的页码
        [scrollViewM setContentOffset:CGPointMake(self.view.bounds.size.width * (pageControl.currentPage),               scrollViewM.contentOffset.y) animated:YES]; //设置scrollview的显示为当前滑动到的页面
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if (offset.x>SCREENWIDTH) {
        pageControl.hidden=YES;
    }
    else
    {
        pageControl.hidden=NO;
    }
}

-(void)pageTurn:(UIPageControl *)aPageControl{
    
    NSInteger whichPage = aPageControl.currentPage;
    
    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDuration:0.3f];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [scrollViewM setContentOffset:CGPointMake(scrollViewM.frame.size.width * whichPage, 0.0f) animated:YES];
    [UIView commitAnimations];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
